package battle
{
	import battle.cmd.IExecutor;
	
	import flash.utils.Dictionary;
	
	import battle.*;

	public class Command
	{
		public static const CMD_TURN_LEFT:int =  1;

		public static const CMD_TURN_RIGHT:int = 2;
		
		public static const CMD_TURN_UP:int = 3;
		
		public static const CMD_TURN_DOWN:int = 4;
		
		public static const CMD_MOVE_LEFT:int = 5;
		
		public static const CMD_MOVE_RIGHT:int = 6;
		
		public static const CMD_MOVE_UP:int = 7;
		
		public static const CMD_MOVE_DOWN:int = 8;
		
		public static const CMD_FIRE:int = 9;
		
		public static const CMD_SKILL:int = 10;
		
		
		protected static var executors:Dictionary = new Dictionary();
		
		
		protected var cmdCode:int;
		protected var context:*;
		protected var target:BaseObject;
		protected var cmdTime:Number;
		
		public function Command( cmdCode:int, context:*, target:BaseObject, cmdTime:int )
		{
			this.cmdCode = cmdCode;
			
			this.context = context;
			
			this.target = target;
			
			this.cmdTime = cmdTime;
			
		}
		
		public function execute():void
		{
			var executor:IExecutor = getExecutor( cmdCode );
			
			if( !executor )
			{
				throw new Error( "unknown command !" );
			}
			
			executor.execute( context, target );
			
		}
		
		public function undo():void
		{
			var executor:IExecutor = getExecutor( cmdCode );
			
			if( !executor )
			{
				throw new Error( "unknown command !" );
			}
			
			executor.undo( context, target );
		}
		
		public function getExecutor( cmdCode:int ):IExecutor
		{
			return IExecutor( executors[ cmdCode ] );
		}
		
		public function registerExecutor( cmdCode:int, executor:IExecutor ):void
		{
			executors[ cmdCode ] = executor;
		}
		
		
	}
}